'use strict'

let lib = {
    vercode: require('../../../modules/lib/vercode'),
    util: require('../../../modules/lib/util')
};

module.exports = {
    setUp(callback) {
        lib.vercode.vercodes.clear();
        lib.vercode.mock({
            myUtil: {
                currentTimestamp() {
                    return 1000;
                }
            }
        });

        callback();
    },
    testPrepare: {
        setUp(callback) {
            lib.vercode.vercodes.clear();
            lib.vercode.mock({
                myUtil: {
                    randomString() {
                        return '7777';
                    },
                    currentTimestamp() {
                        return 1000;
                    }
                }
            });

            callback();
        },
        normal(test) {
            let telephone = '13611111111';

            lib.vercode.prepare(telephone);
            let vercode = lib.vercode.vercodes.get(telephone);

            test.notEqual(vercode, null);
            test.equal(vercode.code, '7777');
            test.equal(vercode.timeout, 1000 + lib.vercode.config.timeout);
            test.equal(vercode.nextTime, 1000 + lib.vercode.config.timeLimit);

            test.done();
        },
        telephoneIsNull(test) {
            let result = lib.vercode.prepare(null);

            test.equal(result, null);
            test.equal(lib.vercode.vercodes.size, 0);

            test.done();
        }
    },
    testCanPrepare: {
        telephoneIsNull(test) {
            let result = lib.vercode.canPrepare(null);

            test.equal(result, false);
            test.done();
        },
        telephoneNotExisted(test) {
            let result = lib.vercode.canPrepare('13611111111');

            test.ok(result);
            test.done();
        },
        telephoneNotReachNextTime(test) {
            let telephone = '13611111111';
            lib.vercode.vercodes.set(telephone, {
                code: '7777',
                timeout: 1200,
                nextTime: 1500
            });

            let result = lib.vercode.canPrepare('13611111111');

            test.equal(result, false);
            test.done();
        },
        telephoneReachNextTime(test) {
            let telephone = '13611111111';
            lib.vercode.vercodes.set(telephone, {
                code: '7777',
                timeout: 800,
                nextTime: 900
            });

            let result = lib.vercode.canPrepare('13611111111');

            test.ok(result);
            test.done();
        }
    },
    testVerify: {
        // telephone为null
        telephoneIsNull(test) {
            let result = lib.vercode.verify(null, '7777');

            test.equal(result, false);
            test.done();
        },
        // code为null
        codeIsNull(test) {
            let result = lib.vercode.verify('13611111111', null);

            test.equal(result, false);
            test.done();
        },
        // telephone存在，code不匹配
        codeMismatched(test) {
            let telephone = '13611111111';
            lib.vercode.vercodes.set(telephone, {
                code: '7777',
                timeout: 1200
            });
            let result = lib.vercode.verify(telephone, '6666');

            test.equal(result, false);
            test.done();
        },
        // telephone存在，code匹配，超时
        allMatchedButTimeout(test) {
            let telephone = '13611111111';
            lib.vercode.vercodes.set(telephone, {
                code: '7777',
                timeout: 800
            });
            let result = lib.vercode.verify(telephone, '7777');

            test.equal(result, false);
            test.done();
        },
        // telephone存在，code匹配，未超时
        allMatchedNotTimeout(test) {
            let telephone = '13611111111';
            lib.vercode.vercodes.set(telephone, {
                code: '7777',
                timeout: 1200
            });
            let result = lib.vercode.verify(telephone, '7777');

            test.ok(result);
            test.done();
        }
    },
    testGc: {
        emptyVercodes(test) {
            lib.vercode.gc();

            test.equal(lib.vercode.vercodes.size, 0);
            test.done();
        },
        clearTimeoutVercode(test) {
            lib.vercode.vercodes.set('13611111111', {
                code: '7777',
                timeout: 800
            });

            lib.vercode.gc();

            test.equal(lib.vercode.vercodes.size, 0);
            test.done();
        },
        skipNoTimeoutVercode(test) {
            let telephone = '13611111111';

            lib.vercode.vercodes.set(telephone, {
                code: '7777',
                timeout: 1200,
                nextTime: 900
            });

            lib.vercode.gc();

            test.equal(lib.vercode.vercodes.size, 1);
            let vercode = lib.vercode.vercodes.get(telephone);
            test.notEqual(vercode, null);
            test.equal(vercode.code, '7777');
            test.equal(vercode.timeout, 1200);
            test.equal(vercode.nextTime, 900);

            test.done();
        }
    }
};
